<?php

namespace app1\index\controller;

use think\Controller;
use think\captcha\Captcha;

class Index extends Controller
{

    public $modelpost;

    public $modeluser;

    public $user;
  
    
    // 接口的入口函数
    function index()
    {
         //return $this->postlist();
        return view("/index/Index");
    }

    
    // 初始化函数
    public function _initialize()
    {
        $this->user = session('user');
        $this->modelpost = model('Post');
        $this->modeluser = model('User');
    }

    
    // 登录函数
    public function login()
    {   
        $identify = $_POST['identify'];
        $user_name = $_POST['user_name'];
        $user_password = $_POST['user_password'];
        $res = $this->modeluser->login($user_name, $user_password, identify);
        return $res;
    }
    
    
    // 用户详情函数
    public function userDetail(){
        $userDetail = $this->user;
        return json(["userDetail"=>$userDetail,"error"=>0,"info"=>"查询结果"]);
    }
  
    
    //上传函数
    public function upLoad(){
        $name = $_POST['imagename'];
        $data = $this->modeluser->upLoad($name);
        return $data;
    }
   
    
    // 注册函数
    public function regist()
    {
        $user_name = $_POST['user_name'];
        $user_password = $_POST['user_password'];
        $image = $_POST['image'];
        $thumb = $_POST['thumb'];
        $res = $this->modeluser->regist($user_name, $user_password, $image, $thumb);
        return json($res);
    }
    
    
    // 发帖函数
    public function post()
    {
        $title = isset($_POST['title'])? htmlspecialchars($_POST['title']) : '';
        $content = isset($_POST['content'])? htmlspecialchars($_POST['content']) : '';
        $user = $this->user;
        $res = $this->modelpost->post($title,$content,$user);
        return $res;
    }

    
    // 帖子详情函数
    public function postDetail()
    {
        $id = $_GET['id'];
        $postDetail = $this->modelpost->getPageList($id);
        return $postDetail;
    }
    
    
    // 删除帖子函数
    public function postDelete()
    {
        $id = $_POST['id'];
        $user = $this->user;
        $res = $this->modelpost->postDelete($id, $user);
        return $res;
    }    
    
    //验证码函数
    function captcha_img($id = "")
    {
        return '<img src="' . captcha_src($id) .  '" alt="点击更换" onclick="this.src=\''.captcha_src().'?id=\'+Math.random();" />';
    }       
    
    // 帖子列表函数
    public function postList()
    {    
        $page = $_GET['page'];
        $postList = $this->modelpost->postList();
        $s = new Captcha();
     
        return $postList;
    }
    
    
    // 帖子编辑函数
    public function postEdit()
    {
        $user = $this->user;
        $id = isset($_POST['id'])? htmlspecialchars($_POST['id']) : '';
        $title = isset($_POST['title'])? htmlspecialchars($_POST['title']) : '';
        $content = isset($_POST['content'])? htmlspecialchars($_POST['content']) : '';
        $postEdit = $this->modelpost->postEdit($id,$content,$title,$user);
        return $postEdit;
    }

    
    // 帖子回复函数
    public function postReply()
    {
        $id = $_POST['id'];
        $content = $_POST['content'];
        $user = $this->user;
        $postReply = $this->modelpost->postReply($id,$content,$user);
        return $postReply;
    }       

    
    // 退出函数
    public function quit()
    {
        $this->user = NULL;
        session("user", NULL);
        return ["error"=>0, "info"=>"退出成功，欢迎下次再来"];
    }
}